﻿Maiks_RBG = LibStub("AceAddon-3.0"):NewAddon("Maiks_RBG", "AceEvent-3.0", "AceComm-3.0", "AceSerializer-3.0")

locations = {}
enemynames = {}
currentenemyteam = {}
currentenemyteamhex = {}

function Maiks_RBG:OnInitialize()
    self.db = LibStub("AceDB-3.0"):New("MaiksRBGdb", defaults, "Default")
   
    Maiks_RBG:RegisterEvent('CURSOR_UPDATE')
    Maiks_RBG:RegisterEvent('PLAYER_FOCUS_CHANGED')
    Maiks_RBG:RegisterEvent('PLAYER_TARGET_CHANGED')
    Maiks_RBG:RegisterEvent('PLAYER_LOGOUT')
   
    Maiks_RBG:RegisterComm("Maiks_RBG_Enemy")
    Maiks_RBG:RegisterComm("Maiks_RBG_Team")
   
    if Maiks_RBGdb == nil then Maiks_RBGdb = {} end
   
    if Maiks_RBGdb.x == nil then 
        Maiks_RBG_title_frame:ClearAllPoints()
        Maiks_RBG_title_frame:SetPoint("TOP", "UIParent", "BOTTOMLEFT", 200, 200)
    else
        Maiks_RBG_title_frame:ClearAllPoints()
        Maiks_RBG_title_frame:SetPoint("TOP", "UIParent", "BOTTOMLEFT", Maiks_RBGdb.x, Maiks_RBGdb.y);
    end
end

Maiks_RBG_title_frame = CreateFrame("Frame", "DragFrame2", UIParent)
Maiks_RBG_title_frame:SetMovable(true)
Maiks_RBG_title_frame:EnableMouse(true)
Maiks_RBG_title_frame:SetPoint("TOP", "UIParent", "BOTTOMLEFT", 200, 200);
Maiks_RBG_title_frame:SetWidth(200); 
Maiks_RBG_title_frame:SetHeight(20);
Maiks_RBG_title_frame:SetParent("UIParent")

local tex1 = Maiks_RBG_title_frame:CreateTexture("ARTWORK");
tex1:SetAllPoints();
tex1:SetTexture(0, 0, 0.3); 
tex1:SetAlpha(0.5);

Maiks_RBG_title_frame:SetScript("OnMouseDown", function(self, button)
      if button == "LeftButton" and not self.isMoving then
         self:StartMoving();
         self.isMoving = true;
      end
end)
Maiks_RBG_title_frame:SetScript("OnMouseUp", function(self, button)
      if button == "LeftButton" and self.isMoving then
         self:StopMovingOrSizing();
         self.isMoving = false;
      end
end)
Maiks_RBG_title_frame:SetScript("OnHide", function(self)
      if ( self.isMoving ) then
         self:StopMovingOrSizing();
         self.isMoving = false;
      end
end)

Maiks_RBG_frame = CreateFrame("Frame", "DragFrame2", Maiks_RBG_title_frame)
Maiks_RBG_frame:SetMovable(true)
Maiks_RBG_frame:EnableMouse(true)
Maiks_RBG_frame:SetPoint("TOP", Maiks_RBG_title_frame, "BOTTOM", 0, 0); 
Maiks_RBG_frame:SetWidth(200); 
Maiks_RBG_frame:SetHeight(400);

local tex2 = Maiks_RBG_frame:CreateTexture("ARTWORK");
tex2:SetAllPoints();
tex2:SetTexture(0, 0, 0); 
tex2:SetAlpha(0.3);

label1 = Maiks_RBG_frame:CreateFontString("label1");
label1:SetFont("Fonts\\FRIZQT__.TTF", 10);
label1:SetPoint("TOPLEFT", Maiks_RBG_frame, "TOPLEFT", 5, -5);
label1:SetJustifyH("LEFT")
label1:SetText("No BG-Team");

label2 = Maiks_RBG_frame:CreateFontString("label1");
label2:SetFont("Fonts\\FRIZQT__.TTF", 10);
label2:SetPoint("TOPLEFT", label1, "BOTTOMLEFT", 0, 0);
label2:SetJustifyH("LEFT")
label2:SetText("\nNo Enemy's");

title = Maiks_RBG_frame:CreateFontString("title");
title:SetFont("Fonts\\FRIZQT__.TTF", 18);
title:SetPoint("BOTTOM", Maiks_RBG_frame, "top", 0, 0);
title:SetJustifyH("CENTER")
title:SetText("Maiks: RBG");

Maiks_RBG_frame:SetHeight((ceil(label1:GetTop())-ceil(label2:GetBottom()))+10);

function GetHex(englishClass)
    if englishClass == "HUNTER" then hexcolor = "ABD473"
    elseif englishClass == "WARLOCK" then hexcolor = "ABD473"
    elseif englishClass == "PRIEST" then hexcolor = "FFFFFF"
    elseif englishClass == "PALADIN" then hexcolor = "F58CBA"
    elseif englishClass == "MAGE" then hexcolor = "69CCF0"
    elseif englishClass == "ROGUE" then hexcolor = "7f7f7f"
    elseif englishClass == "DRUID" then hexcolor = "FF7D0A"
    elseif englishClass == "SHAMAN" then hexcolor = "0070DE"
    elseif englishClass == "WARRIOR" then hexcolor = "C79C6E"
    elseif englishClass == "DEATHKNIGHT" then hexcolor = "C41F3B"
    end
    return hexcolor
end


function ActionButton_OnUpdate()
	if (Maiks_RBG_LastTime == nil) then
	    Maiks_RBG_LastTime = GetTime()
	else
		if  (GetTime() >= Maiks_RBG_LastTime + 5.0) then
            Maiks_RBG_LastTime = GetTime()
            if UnitInBattleground("player") == nil then 
                currentteam = {};
                locations = {};
                enemynames = {};
                currentenemyteam = {};
                currentenemyteamhex = {};
                label1:SetText("No BG-Team");
                label2:SetText("\nNo Enemy's");
                Maiks_RBG_frame:SetHeight((ceil(label1:GetTop())-ceil(label2:GetBottom()))+10);
            elseif GetBattlefieldInstanceRunTime() >=60000 then
                Maiks_RBG:TeamRange()   
            end
		end
	end
end 

function Maiks_RBG:TeamRange()
    local raidnames = nil
    local zone;
    if GetNumRaidMembers() >= 1 then
        for i=1,GetNumRaidMembers() do 
            if UnitInRange("raid"..i) then
                if raidnames == nil then raidnames = strtrim(GetUnitName("raid"..i, false), " (*)") else raidnames = raidnames.."_"..strtrim(GetUnitName("raid"..i, false), " (*)") end
            end
        end
        if raidnames == nil then else
            local zone = GetMinimapZoneText()
            local msg = Maiks_RBG:Serialize(raidnames.."|"..zone)
            Maiks_RBG:SendCommMessage("Maiks_RBG_Team", msg, "BATTLEGROUND")
        end
    end
end

function Maiks_RBG:CURSOR_UPDATE()
    local unit = "mouseover"
    if CheckInteractDistance(unit, 1) and UnitIsPlayer(unit) then
        Maiks_RBG:EnemyRange(unit)
    end
end

function Maiks_RBG:PLAYER_FOCUS_CHANGED()
    local unit = "focus"
        if UnitIsPlayer(unit) then
        Maiks_RBG:EnemyRange(unit)
    end
end

function Maiks_RBG:PLAYER_TARGET_CHANGED()
    local unit = "target"
        if UnitIsPlayer(unit) then
        Maiks_RBG:EnemyRange(unit)
    end
end

function Maiks_RBG:PLAYER_LOGOUT()
Maiks_RBGdb.x, Maiks_RBGdb.y = Maiks_RBG_title_frame:GetCenter()
end

function Maiks_RBG:EnemyRange(unit)
    local enemymsg, enemyname, zone;
    if UnitIsEnemy("player",unit) then
    local enemyname = strtrim(GetUnitName(unit, false), " (*)")
        local zone = GetMinimapZoneText()
        local _,englishClass = UnitClass(unit)
        local hex = GetHex(englishClass)
        if enemymsg == enemyname.."|"..zone then else
            local enemymsg = enemyname.."|"..zone.."|"..hex;
            Maiks_RBG:SendCommMessage("Maiks_RBG_Enemy", enemymsg, "BATTLEGROUND")
        end
    end
end

function Maiks_RBG:OnCommReceived(prefix, addonmsg, arg1, arg2)
    if prefix == "Maiks_RBG_Team" then 
        local commbool, teamdata = Maiks_RBG:Deserialize(addonmsg) 
        local teamnames, location = strsplit("|", teamdata)
        locationnames = {strsplit("_", teamnames)}
        if  tContains(locations, location) then else tinsert(locations, location) end
        if currentteam == nil then 
            currentteam = {}
            for z=1,GetNumRaidMembers() do 
                currentname = strtrim(GetUnitName("raid"..z, false), " (*)")
                currentteam[currentname] = location
            end
        end
        for x=1,getn(locationnames) do
            currentteam[locationnames[x]] = location
        end
        teamtext = nil
        for y=1,getn(locations) do
            for z=1,GetNumRaidMembers() do 
                if z == 1 then 
                    if teamtext == nil then teamtext = "  "..locations[y] else teamtext = teamtext.."\n\n  "..locations[y] end
                end
                currentname = strtrim(GetUnitName("raid"..z, false), " (*)")
                if currentteam[currentname] == locations[y] then
                    _,englishClass = UnitClass(currentname)
                    local hexcolor = GetHex(englishClass)
                    teamtext = teamtext.."\n    |cff"..hexcolor..currentname.."|r"
                end
            end
        end
        label1:SetText("RBG-Team:\n\n"..teamtext);
    elseif prefix == "Maiks_RBG_Enemy" then
        local enemyname, location, hex = strsplit("|", addonmsg)
        if  tContains(locations, location) then else tinsert(locations, location) end
        if  tContains(enemynames, enemyname) then else tinsert(enemynames, enemyname) end
        currentenemyteam[enemyname] = location
        currentenemyteamhex[enemyname] = hex
        enemytext = nil
        for y=1,getn(locations) do
            for z=1,getn(enemynames) do
            currentenemyname = enemynames[z]
                if z == 1 then 
                    if enemytext == nil then enemytext = "  "..locations[y] else enemytext = enemytext.."\n\n  "..locations[y] end
                end
                if currentenemyteam[currentenemyname] == locations[y] then
                    hexcolor = currentenemyteamhex[currentenemyname]
                    enemytext = enemytext.."\n    |cff"..hexcolor..currentenemyname.."|r"
                end
            end
        end
        label2:SetText("\n\nEnemy-Team:\n\n"..enemytext);
    end
Maiks_RBG_frame:SetHeight((ceil(label1:GetTop())-ceil(label2:GetBottom()))+10);
end